package com.inspur.cloud.controller;

import com.inspur.cloud.entity.dataspace.AuditLogEntity;
import com.inspur.cloud.entity.dataspace.common.OpEntityResult;
import com.inspur.cloud.entity.dataspace.common.OpResult;
import com.inspur.cloud.entity.dataspace.common.PageResult;
import com.inspur.cloud.entity.dataspace.vo.AuditLogReq;
import com.inspur.cloud.service.dataspace.AuditLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;

@RestController
@RequestMapping("/dataspace/api/v1/audit")
@Api(value = "audit", description = "操作日志")
public class AuditLogController {

    @Autowired
    private AuditLogService auditLogService;

    @GetMapping
    @ApiOperation(value = "日志列表", notes = "对接人：soff")
    @RolesAllowed("Admin")
    public PageResult<AuditLogEntity> list(@ApiParam(value = "日志查询对象")
                                                   AuditLogReq auditLogReq, @ApiParam(value = "页号") Integer pageIndex, @ApiParam(value = "每页数量") Integer pageSize) {
        PageResult<AuditLogEntity> result = new PageResult<>(1, "查询成功");
        if (pageIndex == null || pageIndex < 0 || pageSize == null || pageSize < 1 || pageSize > 200) {
            result.setCode(-1);
            result.setMsg("分页参数不正确");
            return result;
        }
        Page<AuditLogEntity> list = auditLogService.list(auditLogReq,pageIndex,pageSize);
        result.setPageList(list.getContent());
        result.setCount(list.getTotalElements());
        result.setPageTotal(list.getTotalPages());
        return result;
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "日志详情", notes = "对接人：soff")
    @RolesAllowed("Admin")
    public OpEntityResult<AuditLogEntity> get(@ApiParam(value = "日志id")
                                              @PathVariable("id") Integer id) {
        OpEntityResult<AuditLogEntity> result = new OpEntityResult();
        if (id == null || id < 0) {
            result.setCode(-1);
            result.setMsg("日志id不正确");
            return result;
        }
        result.setCode(1);
        result.setMsg("查询成功");
        result.setData(auditLogService.findById(id));
        return result;
    }

    @DeleteMapping()
    @ApiOperation(value = "删除日志", notes = "对接人：soff")
    @RolesAllowed("Admin")
    public OpResult get(@ApiParam(value = "日志删除对象")
                                AuditLogReq auditLogReq) {
        OpResult result = new OpResult();
        result.setCode(1);
        result.setMsg("删除成功");
        auditLogService.deleteLog(auditLogReq);
        return result;
    }
}
